//
//  SingleColorProgressBar.m
//  SKPAD
//
//  Created by 何睿 on 15/6/23.
//  Copyright (c) 2015年 heeeruiii. All rights reserved.
//

#import "SingleColorProgressBar.h"

@implementation SingleColorProgressBar

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _maxValue = 100.0;
        _minValue = 0.0;
        _realValue = 0.0;
        //初始化两个背景图
        UIImage * image = [UIImage imageNamed:@"meter_socket_large_black.png"];
        _backgroundView = [[UIImageView alloc] initWithImage:image];
        _backgroundView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView.backgroundColor = [UIColor clearColor];
        UIImage * image2 = [UIImage imageNamed:@"dropdown_socket.png"];
        _backgroundView2 = [[UIImageView alloc] initWithImage:image2];
        _backgroundView2.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView2.backgroundColor = [UIColor clearColor];
        
        //初始化进度图层
        _trackLayerProgress = [CAShapeLayer layer];
        _trackLayerProgress.bounds = CGRectMake(self.bounds.size.width * 0.05, self.bounds.size.height * 0.0789, self.bounds.size.width * 0.9, self.bounds.size.height * 0.8422);
        _trackLayerProgress.position = CGPointMake(CGRectGetWidth(self.bounds)/2.0, CGRectGetHeight(self.bounds)/2.0);
        _trackLayerProgress.fillColor = [UIColor clearColor].CGColor;
        _trackLayerProgress.strokeColor = [UIColor colorWithRed:8.0/255 green:120.0/255 blue:170.0/255 alpha:1].CGColor;
        _trackLayerProgress.lineCap = kCALineCapRound;
        _trackLayerProgress.strokeStart = 0.f;
        _trackLayerProgress.strokeEnd = 1.0f;
        _trackLayerProgress.lineWidth = frame.size.height * 0.8422;
        //int tempValue = (int)((realValue - _minValue)/(_maxValue - _minValue) * 22);
        UIBezierPath * lineTrajectory = [UIBezierPath bezierPath];
        
        //起点坐标
        [lineTrajectory moveToPoint:CGPointMake(self.bounds.size.width * 0.05, self.bounds.size.height / 2.0)];
        //终点坐标
        [lineTrajectory addLineToPoint:CGPointMake(self.bounds.size.width * 0.95, self.bounds.size.height / 2.0)];
        
        //连线
        _trackLayerProgress.path = lineTrajectory.CGPath;
        
        //画刻度层
        
        [self.layer insertSublayer:_backgroundView2.layer atIndex:2];
        [self.layer insertSublayer:_backgroundView.layer atIndex:0];
        [self.layer insertSublayer:_trackLayerProgress atIndex:1];
        [self setMyRealValue:55.0];
    }
    return  self;
}

- (void)setMyRealValue:(CGFloat)realValue
{
    if (_realValue != realValue)
    {
        _trackLayerProgress.strokeEnd = realValue;
        _realValue = realValue;
        
        
    }
}

@end